//
//  QZHairstyleViewController.m
//  SmallShoppingCenter
//
//  Created by 上官惠阳 on 15/10/14.
//  Copyright (c) 2015年 上官惠阳. All rights reserved.
//

#import "QZHairstyleViewController.h"
#import "QZHairstyleCell.h"
#import "QZHairstyleHead.h"
#import "QZHairDetailViewController.h"
#import "QZShiftView.h"
#import "QZHairStyleModel.h"

@interface QZHairstyleViewController ()

@end

@implementation QZHairstyleViewController
{
    QZHairstyleHead *_head;
    UIView *_grayView;
    QZShiftView *_shiftView;
    
    NSString *_sex;//性别
    NSString *_cateid;//分类ID
    NSString *_lately;//时间排序或者评分排序;
    
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"发型";
    
    self.collectionView.frame = CGRectMake(0, 35, WIDTH, HEIGHT-35 - 64);
    
    CGFloat cellWidth = (WIDTH-18)/2;
    [self registerCellWithNibName:@"QZHairstyleCell" addIdentifier:@"hairCell" addItemSize:CGSizeMake(cellWidth,cellWidth/(152/184.0) ) andItemSpacing:6 andInsetsForSection:UIEdgeInsetsMake(3, 6, 0, 6)];
    self.collectionView.backgroundColor = BACKGROUDCOLOR;
    
    [self createHeadView];
    
    [self createGrayView];
    [self createShiftView];
    
    [self requestHead];
    
    [self addRefreshWithRefreshAnimationWithImages:nil];
}
-(void)requestHead
{
    [self requestWithKeys:nil andValues:nil andUrlKey:@"Hair.GetHairCate" isCache:YES];
}
-(void)requestData
{
    NSLog(@"page = %@",@(self.page));
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",newPublicURL,@"Hair.GetHairList"];
    NSMutableDictionary *dic= [[NSMutableDictionary alloc]initWithDictionary:@{@"page":@(self.page),@"limit":@(20)}];
    if (_sex) {
        [dic setObject:_sex forKey:@"sex"];
    }
    if (_cateid) {
        [dic setObject:_cateid forKey:@"hair_cate_id"];
    }
    if (_lately) {
        [dic setObject:_lately forKey:@"sort"];
    }
    
    [self.request QZRequest_POST:urlStr parameters:dic tagNSString:@"Hair.GetHairList" stopRequest:NO isSerializer:NO isCache:YES];
}
#pragma mark QZRequestProtocol
-(void)request_POST_FinishValue:(id)value tagNSString:(NSString *)tag
{

    if ([tag isEqualToString:@"Hair.GetHairList"]) {
        NSInteger status = [value[@"data"][@"status"] intValue];
        if ( status == 100 || status == 101) {
            if (self.page == 1) {
                [self.collectionSource removeAllObjects];
            }
            
            if (status == 101 && self.page>1) {
                [[iToast makeText:@"无更多数据"] show];
            }
            
            NSArray *data = value[@"data"][@"data"][@"list"];
            if ([data isKindOfClass:[NSArray class]]) {
                for (NSDictionary *dic in data) {
                    QZHairStyleModel *model = [[QZHairStyleModel alloc]initWithDictionary:dic error:nil];
                    [self.collectionSource addObject:model];

                }
            }
            
            [self.collectionView reloadData];
        }
    }else if([tag isEqualToString:@"Hair.GetHairCate"]){
        if ([value[@"data"][@"status"] intValue] == 100) {
            NSArray *data = value[@"data"][@"data"][@"list"];
            for (int i = 0; i < data.count; i ++) {
                NSDictionary *dic = data[i];
                UIButton *btn = _shiftView.faArr[i];
                btn.hidden = NO;
                [btn setTitle:dic[@"CateName"] forState:UIControlStateNormal];
                btn.tag = 100 + [dic[@"CateId"]  intValue];
            }
        }
    }
}
-(void)createGrayView
{
    UIView *grayView = [[UIView alloc]initWithFrame:CGRectMake(0,35, WIDTH,HEIGHT-64-35-49)];
    grayView.backgroundColor = [UIColor blackColor];
    grayView.alpha = 0.5;
    grayView.hidden = YES;
    [self.view addSubview:grayView];
    _grayView = grayView;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [_grayView addGestureRecognizer:tap];
}
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    [self headBtnAction:_head.siftBtn];
}
-(void)createShiftView
{
    QZShiftView *shiftView = [[[NSBundle mainBundle]loadNibNamed:@"QZShiftView" owner:self options:nil]lastObject];
    shiftView.frame = CGRectMake(0,35+1, WIDTH,202);
    shiftView.hidden = YES;
    for (UIButton *btn in shiftView.sexBtns) {
        [btn addTarget:self action:@selector(sexBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    for (UIButton *btn in shiftView.faArr) {
        [btn addTarget:self action:@selector(faAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    [shiftView.sureBtn addTarget:self action:@selector(sureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    _shiftView = shiftView;
    [self.view addSubview:shiftView];
}
-(void)sureBtnAction:(UIButton *)btn
{
   [self headBtnAction:_head.siftBtn];
    self.page = 1;
    [self requestData];
}
-(void)sexBtnAction:(UIButton *)btn
{
    if (btn.selected == NO) {
        btn.selected = YES;
        if (btn == _shiftView.manBtn) {
            _shiftView.womanBtn.selected = NO;
            _sex = @"1";
        }else{
            _shiftView.manBtn.selected = NO;
            _sex = @"2";
        }
    }else{
        btn.selected = NO;
        _sex = nil;
    }
    
    self.page = 1;
}
-(void)faAction:(UIButton *)btn
{
    if (btn.selected == NO) {
        btn.selected = YES;
        btn.backgroundColor = RGBCOLOR(0.93, 0.64, 0.99);
        _cateid = [NSString stringWithFormat:@"%ld",btn.tag - 100];
        //其他的都不选中
        for (UIButton *abtn in _shiftView.faArr) {
            if (abtn != btn) {
                abtn.selected = NO;
                abtn.backgroundColor = [UIColor clearColor];
            }
        }
    }else{
        btn.selected = NO;
        btn.backgroundColor = [UIColor clearColor];
        _cateid = nil;
    }
    
}
-(void)createHeadView
{
    _head = [[[NSBundle mainBundle]loadNibNamed:@"QZHairstyleHead" owner:self options:nil]lastObject];
    _head.frame = CGRectMake(0,0, WIDTH, 35);
    for (UIButton *btn in _head.btns) {
        [btn addTarget:self action:@selector(headBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    [self.view addSubview:_head];
}
-(void)headBtnAction:(UIButton *)btn
{
    if (btn == _head.siftBtn) {//如果点击的是筛选按钮
        btn.selected = !btn.selected;
        if (_head.siftBtn.selected) {
            _grayView.hidden = NO;
            _shiftView.hidden = NO;
            _head.redSan.image = [UIImage imageNamed:@"下拉-选中"];
        }else{
            _grayView.hidden = YES;
            _shiftView.hidden = YES;
            _head.redSan.image = [UIImage imageNamed:@"矩形-115-拷贝"];
        }
        _head.mostNewBtn.selected = NO;
        _head.markBtn.selected = NO;
    }else{
        for (UIButton *btn in _head.btns) {
            btn.selected = NO;
        }
        btn.selected = YES;
        _grayView.hidden = YES;
        _shiftView.hidden = YES;
        _head.redSan.image = [UIImage imageNamed:@"下拉-未选"];
        
        if (_head.mostNewBtn == btn) {
            //最新按钮
            _lately = @"time";
        }else if(_head.markBtn == btn){
            //评分按钮
            _lately = @"people";
            
        }
        self.page = 1;
        [self requestData];
    }
}
-(void)loadCell:(UICollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    QZHairStyleModel *model = self.collectionSource[indexPath.row];
    QZHairstyleCell *hairCell = (QZHairstyleCell *)cell;
    [hairCell loadCellWithModel:model];
}
-(void)actionAtIndexPath:(NSIndexPath *)indexPath
{
    QZHairStyleModel *model = self.collectionSource[indexPath.row];
    QZHairDetailViewController *detialVc = [[QZHairDetailViewController alloc]init];
    detialVc.hidesBottomBarWhenPushed = YES;
    detialVc.userId = model.user_id;
    detialVc.hairFormId = model.id;
    [self.navigationController pushViewController:detialVc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
